import Vue from "vue";
import Router from "vue-router";
import findLast from "lodash/findLast";
import { notification } from "ant-design-vue";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import NotFound from "./views/404";
import Forbidden from "./views/403";
import { check, isLogin } from "./utils/auth";

Vue.use(Router);

const router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/user",
      hideInMenu: true,
      component: () =>
        import(/* webpackChunkName: "layout" */ "./layouts/UserLayout"),
      children: [
        {
          path: "/user",
          redirect: "/user/login"
        },
        {
          path: "/user/login",
          name: "login",
          component: () =>
            import(/* webpackChunkName: "user" */ "./views/User/Login")
        },
        {
          path: "/user/register",
          name: "register",
          component: () =>
            import(/* webpackChunkName: "user" */ "./views/User/Register")
        }
      ]
    },
    {
      path: "/",
      meta: { authority: ["user", "admin"] },
      component: () =>
        import(/* webpackChunkName: "layout" */ "./layouts/BasicLayout"),
      children: [
        // dashboard
        {
          path: "/",
          redirect: "/dashboard/analysis"
        },
        {
          path: "/dashboard",
          name: "dashboard",
          meta: { title: "首页" },
          component: { render: h => h("router-view") },
          children: [
            {
              path: "/dashboard/analysis",
              name: "analysis",
              meta: {icon: "dashboard", title: "分析页" },
              component: () =>
                import(/* webpackChunkName: "dashboard" */ "./views/Dashboard/Analysis")
            }
          ]
        },
        // 警员管理
        {
          path: "/policeinfo",
          name: "policeinfo",
          meta: { title: "警员管理"},
          component: { render: h => h("router-view") },
          children: [
            {
              path: "/policeinfo/policemanag",
              name: "policemanag",
              meta: { icon: "usergroup-add", title: "管理警员" },
              component: () =>
                import(/* webpackChunkName: "dashboard" */ "./views/Policemanag/PoliceInfo")
            }
          ]
        },
        
        // 标准地址Standard address<a-icon type="" theme="twoTone" />
        {
          path: "/address",
          name: "address",
          meta: { title: "标准地址数据录入"},
          component: { render: h => h("router-view") },
          children: [
            {
              path: "/address/standard",
              name: "standard",
              meta: { icon: "environment",title: "标准地址" },
              component: () =>
                import(/* webpackChunkName: "dashboard" */ "./views/Address/Standard")
            }
          ]
        },
        // 信息管理<a-icon type="info-circle" theme="filled" />
        {
          path: "/info",
          name: "info",
          meta: { title: "上报信息管理"},
          component: { render: h => h("router-view") },
          children: [
            {
              path: "/info/infomanage",
              name: "infomanage",
              meta: {icon: "info-circle", title: "信息管理" },
              component: () =>
                import(/* webpackChunkName: "dashboard" */ "./views/Info/Infomanage")
            },
            {
              path: "/info/bell",
              name: "bell",
              meta: {icon: "bell",  title: "情报信息" },
              component: () =>
                import(/* webpackChunkName: "dashboard" */ "./views/Info/Bell")
            }
            ,
            {
              path: "/info/dispute",
              name: "dispute",
              meta: {icon: "stop",  title: "矛盾纠纷" },
              component: () =>
                import(/* webpackChunkName: "dashboard" */ "./views/Info/Dispute")
            }
          ]
        },
        // 义警管理
        {
          path: "/vigilantes",
          name: "vigilantes",
          meta: { title: "义警任务管理"},
          component: { render: h => h("router-view") },
          children: [
            {
              path: "/vigilantes/VigilantesInfo",
              name: "VigilantesInfo",
              meta: { icon: "flag", title: "义警管理" },
              component: () =>
                import(/* webpackChunkName: "dashboard" */ "./views/Vigilantes/VigilantesInfo")
            }
          ]
        },
        // 公告 文章管理<a-icon type="profile" theme="twoTone" />
        {
          path: "/article",
          name: "article",
          meta: { title: "新闻及通知通告管理"},
          component: { render: h => h("router-view") },
          children: [
            {
              path: "/article/essay",
              name: "essay",
              meta: { icon: "profile", title: "文章管理" },
              component: () =>
                import(/* webpackChunkName: "dashboard" */ "./views/Article/Essay/Essay")
            },
            {
              path: "/article/notice",
              name: "notice",
              meta: { icon: "notification",title: "公告管理" },
              component: () =>
                import(/* webpackChunkName: "dashboard" */ "./views/Article/Notice/Notice")
            }
          ]
        },
        // 咨询管理
        {
          path: "/consult",
          name: "consult",
          meta: {  title: "留言咨询管理"},
          component: { render: h => h("router-view") },
          children: [
            {
              path: "/consult/advisory",
              name: "advisory",
              meta: { icon: "message",title: "咨询管理" },
              component: () =>
                import(/* webpackChunkName: "dashboard" */ "./views/Consult/Advisory/index")
            },
            {
              path: "/consult/mail",
              name: "mail",
              meta: {icon: "mail", title: "留言管理" },
              component: () =>
                import(/* webpackChunkName: "dashboard" */ "./views/Consult/Mail")
            }
          ]
        },
        // 高级管理
        {
          path: "/Setting",
          name: "Setting",
          meta: { title: "高级管理"},
          component: { render: h => h("router-view") },
          children: [
            {
              path: "/Setting/jurisdiction",
              name: "jurisdiction",
              meta: {icon: "setting", title: "权限管理" },
              component: () =>
                import(/* webpackChunkName: "dashboard" */ "./views/Setting/Jurisdiction")
            }
          ]
        },
        // form
        // {
        //   path: "/form",
        //   name: "form",
        //   component: { render: h => h("router-view") },
        //   meta: { icon: "form", title: "表单", authority: ["admin"] },
        //   children: [
        //     {
        //       path: "/form/basic-form",
        //       name: "basicform",
        //       meta: { title: "基础表单" },
        //       component: () =>
        //         import(/* webpackChunkName: "form" */ "./views/Forms/BasicForm")
        //     },
        //     {
        //       path: "/form/step-form",
        //       name: "stepform",
        //       hideChildrenInMenu: true,
        //       meta: { title: "分布表单" },
        //       component: () =>
        //         import(/* webpackChunkName: "form" */ "./views/Forms/StepForm"),
        //       children: [
        //         {
        //           path: "/form/step-form",
        //           redirect: "/form/step-form/info"
        //         },
        //         {
        //           path: "/form/step-form/info",
        //           name: "info",
        //           component: () =>
        //             import(/* webpackChunkName: "form" */ "./views/Forms/StepForm/Step1")
        //         },
        //         {
        //           path: "/form/step-form/confirm",
        //           name: "confirm",
        //           component: () =>
        //             import(/* webpackChunkName: "form" */ "./views/Forms/StepForm/Step2")
        //         },
        //         {
        //           path: "/form/step-form/result",
        //           name: "result",
        //           component: () =>
        //             import(/* webpackChunkName: "form" */ "./views/Forms/StepForm/Step3")
        //         }
        //       ]
        //     }
        //   ]
        // }
      ]
    },
    {
      path: "/403",
      name: "403",
      hideInMenu: true,
      component: Forbidden
    },
    {
      path: "*",
      name: "404",
      hideInMenu: true,
      component: NotFound
    }
  ]
});

router.beforeEach((to, from, next) => {
  if (to.path !== from.path) {
    NProgress.start();
  }
  const record = findLast(to.matched, record => record.meta.authority);
  if (record && !check(record.meta.authority)) {
    if (!isLogin() && to.path !== "/user/login") {
      next({
        path: "/user/login"
      });
    } else if (to.path !== "/403") {
      notification.error({
        message: "403",
        description: "你没有权限访问，请联系管理员咨询。"
      });
      next({
        path: "/403"
      });
    }
    NProgress.done();
  }

  next();
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
